<?php

namespace app\api\model;

use app\common\enums\StatusEnum;

class OauthSession extends \app\common\model\OauthSession
{
    /**
     * 通过用户id获取记录
     * @return false|\think\db\Query[]
     * @throws \think\Exception\DbException
     */
    public static function getSessionsByUserId($userId)
    {
        $model = static::field('*')
            ->where(['oauth_id' => $userId])
            ->select();
        return $model;
    }
    /**
     * 通过用户id和来源查找
     */
    public static function getSessionsByUserIdAndSource($userId,$source)
    {
        $model = static::field('*')
            ->where(['oauth_id' => $userId,'source'=>$source])
            ->select();
        return $model;
    }

    /**
     * 通过token获取记录
     * @return false|\think\db\Query[]
     * @throws \think\Exception\DbException
     */
    public static function getSessionByToken($token)
    {

        dump($token);exit;
        $model = static::field('*')
            ->where(['token' => $token])
            ->find();

        dump($model);exit;
        return $model;
    }

    /**
     * 删除session
     */
    public static function deleteSession($userId,$id){
        $where =array(
            'oauth_id' => $userId,
            'id'=>$id
        );
        $ret = static::where($where)->delete();
        return $ret;
    }

    /**
     * 添加缓存
     */
    public static function addSession($token,$userId,$overtime){
        $model = static::create([
            'oauth_id' => $userId,
            'token' => $token,
            'overtime' => $overtime,
            'source'=>checkMiniprogram() ? 2 : 1,
        ]);
        return $model->id;
    }

    /**
     * 添加缓存
     */
    public static function addNewSession($token,$userId,$overtime,$source){
        $model = static::create([
            'oauth_id' => $userId,
            'token' => $token,
            'overtime' => $overtime,
            'source'=>$source
        ]);
        return $model->id;
    }

}